package com.seleniti.enpareja.web.desktop.server.common.util;

import java.io.InputStreamReader;
import java.security.NoSuchAlgorithmException;

public class CryptoUtils {
	public static final String SECURITY_SYSTEM = "SHA-1";

	public static void main(String arg[]) {
		try {
			// quick way to do input from the keyboard, now deprecated...
			java.io.StreamTokenizer Input = new java.io.StreamTokenizer(new InputStreamReader(System.in));
			//
			System.out.print("Input your secret password : ");
			Input.nextToken();
			String hash = byteArrayToHexString(CryptoUtils.computeHash(Input.sval));
			System.out.println("the computed hash (hex string) : " + hash);
			boolean ok = true;
			String inputHash = "";
			while (ok) {
				System.out.print("Now try to enter a password : ");
				Input.nextToken();
				inputHash = byteArrayToHexString(CryptoUtils.computeHash(Input.sval));
				if (hash.equals(inputHash)) {
					System.out.println("You got it!");
					ok = false;
				} else
					System.out.println("Wrong, try again...!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String generateUUID() {
		return java.util.UUID.randomUUID().toString();
	}

	public static String encrypt(String word) throws NoSuchAlgorithmException {
		return byteArrayToHexString(computeHash(word));
	}

	protected static byte[] computeHash(String x) throws NoSuchAlgorithmException {
		java.security.MessageDigest d = null;
		d = java.security.MessageDigest.getInstance(SECURITY_SYSTEM);
		d.reset();
		d.update(x.getBytes());
		return d.digest();
	}

	protected static String byteArrayToHexString(byte[] b) {
		StringBuffer sb = new StringBuffer(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			int v = b[i] & 0xff;
			if (v < 16) {
				sb.append('0');
			}
			sb.append(Integer.toHexString(v));
		}
		return sb.toString().toUpperCase();
	}

}
